package com.qwe.graduation.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qwe.graduation.entity.dto.UserDto;
import com.qwe.graduation.entity.vo.UserVo;
import com.qwe.graduation.shiro.utils.JwtUtil;
import com.qwe.graduation.utils.exception.Constants;
import com.qwe.graduation.utils.exception.Result;
import com.qwe.graduation.utils.exception.ServiceException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.qwe.graduation.service.IUserService;
import com.qwe.graduation.entity.User;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 林卓锋
 * @since 2023-01-08
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService IUserService;

    //新增或更改
    @GetMapping("/hello")
    public String getName() {

        return "hello world!";
    }

    @GetMapping("/list")
    public List<User> list() {
        List<User> list = IUserService.list();
        return list;
    }

    @PostMapping("/save")
    public Result add(@RequestBody User user) {
        if (StringUtils.isBlank(user.getNickName())) {
            throw new ServiceException(Constants.CODE_400, "用户昵称不能为空！");
        }
        user.setUpdateTime(new Date());
        boolean b = IUserService.saveOrUpdate(user);
        return Result.success();
    }

    @PostMapping("/update")
    public Boolean update(@RequestBody User user) {
        return IUserService.updateById(user);
    }

    @GetMapping("/name/{username}")
    public Result getOne(@PathVariable String username) {
        QueryWrapper<User> wrapper= new QueryWrapper<>();
        wrapper.eq("name", username);
        User one = IUserService.getOne(wrapper);
        UserVo userVo=new UserVo();
        BeanUtils.copyProperties(one,userVo);
        userVo.setUsername(one.getName());
        return Result.success(userVo);
    }

    @DeleteMapping("/delete/{id}")
    public Boolean delete(@PathVariable String id) {
        return IUserService.removeById(id);
    }

    @PostMapping("/delete/Beatch")
    public Boolean deleteBeatch(@RequestBody List<String> ids) {
        return IUserService.removeByIds(ids);
    }

    //分页
    @GetMapping("/page")
    public Result page(@RequestParam Integer pageNum,
                                                @RequestParam Integer pageSize,
                                                @RequestParam(defaultValue = "", required = false) String name,
                                                @RequestParam(defaultValue = "", required = false) String userPhone,
                                                HttpServletRequest request) {
        if (userPhone==""){
            userPhone=null;
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper();
        QueryWrapper<User> name1 = queryWrapper
                 .orderByDesc("update_time") // 这里应该要根据时间倒叙,数据库完善后要改一下
                .like("user_phone", userPhone)
                .like("name", name);

        Page page = new Page(pageNum, pageSize);
        Page page1 = IUserService.page(page, name1);

        return Result.success(page1);
    }

    //登录
    @PostMapping("/login")
    public Result login(@RequestBody UserDto userDTO) {
        String username = userDTO.getUsername();
        String password = userDTO.getPassword();
        if (StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
            return Result.error(Constants.CODE_400,"参数错误");
        }
        UserDto login = IUserService.login(userDTO);
        return Result.success(login);
    }

    //注册
    @PostMapping("/register")
    public Result register(@RequestBody UserDto userDTO) {
        String username = userDTO.getUsername();
        String password = userDTO.getPassword();
        if (StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
            return Result.error(Constants.CODE_400, "参数错误");
        }
        return Result.success(IUserService.register(userDTO));
    }


    /**
     *首页
     * @return
     */
    @GetMapping("/index")
    public Result index(){

        return Result.success();
    }
}
